package com.earnings.okhttputils.utils.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.earnings.R;
import com.earnings.okhttputils.utils.UrlConstant.Constant;


public abstract class PaytypeDialog extends Dialog implements
		View.OnClickListener {
	private Activity act;
	private LinearLayout layout;
	private LinearLayout mView;
	private String typename;
	private View right1,right2,right3,right4;
	private String typeid;
	/**
	 * 选择支付类型
	 *
	 * @param context
	 */
	public PaytypeDialog(Context context) {
		super(context, R.style.dialog_msg);
		this.act = (Activity) context;

		mView = (LinearLayout) act.getLayoutInflater().inflate(R.layout.dialog_paytypes,
				null);
		layout= (LinearLayout) mView.findViewById(R.id.cont);
		layout.getLayoutParams().width = act.getWindowManager()
				.getDefaultDisplay().getWidth();

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		mView.findViewById(R.id.moneypay).setOnClickListener(this);
		mView.findViewById(R.id.alpay).setOnClickListener(this);
		mView.findViewById(R.id.treasurepay).setOnClickListener(this);
		mView.findViewById(R.id.wxpay).setOnClickListener(this);
		mView.findViewById(R.id.back).setOnClickListener(this);
		right1=mView.findViewById( R.id.right1 );
		right2=mView.findViewById( R.id.right2 );
		right3=mView.findViewById( R.id.right3 );
		right4=mView.findViewById( R.id.right4 );
		typeid= Constant.PAY_TYPE_BLANANCE;
		typename = "余额支付";
		setChoose(typeid);
	}



	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(layout);
		this.setCancelable(true);// 可以用“返回键”取消
		Window window = this.getWindow();
		window.setContentView(mView);
		window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
		window.setWindowAnimations(R.style.dialog_right_anim);
		WindowManager m = act.getWindowManager();
		Display d = m.getDefaultDisplay();  //为获取屏幕宽、高
		android.view.WindowManager.LayoutParams p =getWindow().getAttributes();  //获取对话框当前的参数值
		p.height = (int) (d.getHeight() * 0.5);   //高度设置为屏幕的0.5
		getWindow().setAttributes(p);     //设置生效
	}

	public void cencelBanlance(){
		typeid = Constant.PAY_TYPE_WEIXIN;

		setChoose(typeid);
		typename = "微信支付";
		mView.findViewById(R.id.moneypay).setVisibility( View.GONE );
		mView.findViewById(R.id.treasurepay).setVisibility( View.GONE );
	}

	public void showTreasurepay(){
		mView.findViewById(R.id.treasurepay).setVisibility( View.VISIBLE );

	}
	@Override
	public void dismiss() {
		// TODO 自动生成的方法存根
		super.dismiss();
		// TextView tv=(TextView) act.findViewById(R.id.province);
		// tv.setText(province);
	}

	public abstract void chooseItem(String typeid,String typename);
	@Override
	public void onClick(View v) {
		int i = v.getId();
		if (i == R.id.moneypay) {
			typeid = Constant.PAY_TYPE_BLANANCE;
			typename = "余额支付";

		} else if (i == R.id.treasurepay) {
			typeid = Constant.PAY_TYPE_CAIBAO;
			typename = "财币支付";

		} else if (i == R.id.wxpay) {
			typeid = Constant.PAY_TYPE_WEIXIN;
			typename = "微信支付";

		} else if (i == R.id.alpay) {
			typeid = Constant.PAY_TYPE_ALIPAY;
			typename = "支付宝支付";

		}else if (i == R.id.back) {
			dismiss();

		}
		setChoose(typeid);
		chooseItem(typeid,typename);
		dismiss();
	}

	public void sumbit(View.OnClickListener listener)
	{
    	mView.findViewById(R.id.sumbit).setOnClickListener(listener);
	}


	public void setChoose(String name){
		right1.setVisibility( View.GONE );
		right2.setVisibility( View.GONE );
		right3.setVisibility( View.GONE );
		right4.setVisibility( View.GONE );
		if(name.equals( Constant.PAY_TYPE_BLANANCE ))
		right1.setVisibility( View.VISIBLE );
		else if(name.equals( Constant.PAY_TYPE_CAIBAO ))
			right2.setVisibility( View.VISIBLE );
		else if(name.equals( Constant.PAY_TYPE_WEIXIN))
			right3.setVisibility( View.VISIBLE );
		else if(name.equals( Constant.PAY_TYPE_ALIPAY ))
			right4.setVisibility( View.VISIBLE );
	}


}
